

import cscope
import os, _pg
import mailbox, project



def setupTables(db):
    q = """
CREATE TABLE functions(
	funcID		serial		NOT NULL,
	name		varchar(100) 	NOT NULL,
	startline	int		NOT NULL,
	endline		int		NOT NULL,
	FileID		int		references Files(FileID) NOT NULL,
	interface	varchar(500)	NOT NULL,
PRIMARY KEY  (funcid)
);
"""
    dropq = """
drop table functions cascade;
"""
    try:
        print "cleaning Function tables..."
        db.query(dropq)
        print "...OK!"
    except _pg.ProgrammingError:
            print "could not drop tables"
    try:
        print "creating Function tables..."
        db.query(q)
        print "...OK!"
    except _pg.ProgrammingError, e:
        print "could not create tables"
        raise project.ProjectError("Could not create tables, cause:" + str(e))

def start(proj, log):
    repo = proj.getSVNRepository()
    db = proj.getDatabase()
    setupTables(db)
    log.write("Starting to mine and store functions...")
    cscope.storeFunctions(db, repo, log)
    log.write("...complete")
